﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;

namespace ChineseSchool.UserControl.ThuNghiem
{
    public partial class MainMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMainMenu.Text = LoadMenuTop();
            }
        }

        public string LoadMenuTop()
        {
            //<ul class="nav navbar-nav">
            //        <li><a class="active" href="index.html">Home</a></li>
            //        <li class="dropdown">
            //            <a href="#">Blog <i class="fa fa-caret-down"></i></a>
            //            <ul class="sub-menu">
            //                <li><a href="blog.html">Blog 1</a></li>
            //                <li><a href="blog-2.html">Blog 2</a></li>
            //                <li><a href="blog-3.html">Blog 3</a></li>
            //            </ul>
            //            <!-- /.sub-menu -->
            //        </li>
            //        <!-- /.dropdown -->
            //        <li><a href="single.html">Single</a></li>
            //        <li><a href="contact.html">Contact</a></li>
            //        <li class="dropdown"><a href="#">Category <i class="fa fa-caret-down"></i></a>
            //            <ul class="sub-menu">
            //                <li><a href="category-1.html">Category 1</a></li>
            //                <li><a href="category-2.html">Category 2</a></li>
            //                <li><a href="#">More <i class="fa fa-caret-right"></i></a>
            //                    <ul class="sub-menu-2">
            //                        <li><a href="#">Demo Link 1</a></li>
            //                        <li><a href="#">Demo Link 2</a></li>

            //                        <li><a href="#">More <i class="fa fa-caret-right"></i></a>
            //                            <ul class="sub-menu-3">
            //                                <li><a href="#">Demo Link 1</a></li>
            //                                <li><a href="#">Demo Link 2</a></li>
            //                                <li><a href="#">Demo Link 3</a></li>
            //                            </ul>
            //                            <!-- /.sub-menu-3 -->
            //                        </li>
            //                        <li><a href="#">Demo Link 3</a></li>
            //                    </ul>
            //                    <!-- /.sub-menu-2 -->
            //                </li>
            //            </ul>
            //            <!-- /.sub-menu -->
            //        </li>
            //        <!--  /.dropdown -->
            //        <li><a href="404.html">404</a></li>
            //    </ul>
            var strMenu = "";
            var dt = new B_Menu().GetAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                const int menuChaId = 0;
                const int menuTypeId = 1;
                var lstMenuCha = new B_Menu().GetBy_MenuParentID(menuChaId, menuTypeId);
                strMenu += "<ul class=\"nav navbar-nav\">";
                for (var i = 0; i < lstMenuCha.Count; i++)
                {
                    //Cấp 1
                    var menuId = lstMenuCha[i].menuID;
                    var lstMenuCon = (new B_Menu()).GetBy_MenuParentID(menuId, menuTypeId);
                    if (lstMenuCon != null && lstMenuCon.Count > 0)
                    {
                        //Nếu có cấp 2                        
                        strMenu += "<li class=\"dropdown\"><a href=\"" + Util.MakeLinkMenuInfo(lstMenuCha[i].menuID.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(lstMenuCha[i].menuName)) + "\">" + lstMenuCha[i].menuName + "<i class=\"fa fa-caret-down\"></i></a>";
                        strMenu += "<ul class=\"sub-menu\">";
                        for (var j = 0; j < lstMenuCon.Count; j++)
                        {
                            //Cấp 2
                            var menuIdCon = lstMenuCon[i].menuID;
                            var lstMenuConNext = (new B_Menu()).GetBy_MenuParentID(menuIdCon, menuTypeId);
                            if (lstMenuConNext != null && lstMenuConNext.Count > 0)
                            {
                                //Nếu có cấp 3
                                strMenu += "<li class=\"dropdown\"><a href=\"" + Util.MakeLinkMenuInfo(lstMenuCha[i].menuID.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(lstMenuCha[i].menuName)) + "\">" + lstMenuCha[i].menuName + "<i class=\"fa fa-caret-right\"></i></a>";
                                strMenu += "<ul class=\"sub-menu-2\">";
                                for (var k = 0; k < lstMenuConNext.Count; k++)
                                {

                                    strMenu += "<li ><a href=\"" + Util.MakeLinkMenuInfo(lstMenuConNext[k].menuID.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(lstMenuConNext[k].menuName)) + "\">" + lstMenuConNext[k].menuName + "</a>";

                                }
                                strMenu += "</li> ";
                                strMenu += "</ul> ";
                                //end Cấp 3
                            }
                            else
                            {
                                //Ko có cấp 3
                                strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenuCon[j].menuID.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(lstMenuCon[j].menuName)) + "\">" + lstMenuCon[j].menuName + "</a>";
                            }
                        }
                        strMenu += "</li> ";
                        strMenu += "</ul> ";
                        //end Cấp 2
                    }
                    else
                    {
                        //ko có cấp 2
                        strMenu += "<li><a href=\"" + Util.MakeLinkMenuInfo(lstMenuCha[i].menuID.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(lstMenuCha[i].menuName)) + "\">" + lstMenuCha[i].menuName + "</a>";
                    }
                    strMenu += "</li> ";
                }
                strMenu += "</ul> ";
                //end cấp 1
            }
            return strMenu;
        }
    }
}